package ThreadDemo;

/*两个线程中，
synchronized对同一个类的不通过对象的类对象进行加锁
可以解决线程安全问题*/
import java.util.Scanner;

public class ThreadDemo15 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Object o=new Object();
        Object o2=new Object();
        Thread t = new Thread() {
            @Override
            public void run() {
                synchronized (o.getClass()){
                    System.out.println("请输入一个整数");
                    int num=sc.nextInt();
                    System.out.println(num);
                }
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                while(true){

                    synchronized (o2.getClass()){
                        try {
                            sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("线程2获取到了锁");
                    }
                }
            }
        };

        t2.start();
        t.start();
    }
}
